package GDoc::Job::Fetch::US::Exec::HS::FA::TRS;

use Moose;
use WWW::Mechanize;

use GDoc::Job;

extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'https://www.usfa.dhs.gov/applications/publications/display.cfm?id=&mc=29&currentPage=' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        foreach my $page( 1..15 ) { 
            $self->logger->info( "fetching search result page " . $self->_baseurl . $page );
            my $mech = $self->_mech;
            $mech->get( $self->_baseurl . $page );

            my $html = $mech->response->content;
            
            my @pdf_links = $self->_parse_links( $html );
            foreach my $link( @pdf_links ) { 
                my $job = GDoc::Job->new( class => 'Fetch::US::Exec::HS::FA::TRS',
                                          queue => $self->fetch_queue,
                                          data  => $link
                                        );

                $job->submit;
            }
        }
    }
}


sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    $self->logger->info( "Fetching PDF $data{link}" );
    my $mech = $self->_mech;
    $mech->get( $data{link} );

    $data{link} =~ m{tr[-_](?<num>\d+)}i;

    my $ident     = 'FA-TR-' . $+{num};
    my $filename  = $ident . '.pdf';
    my $key       = 'us/exec/hs/fa/' . $filename;

    $self->logger->info( "Storing $data{link} --> $key" );

    $self->store_pdf( key       => $key,
                      data      => $mech->response->content,
                      filename  => $filename,
                      meta      => { description  => $data{desc},
                                     identifier   => $ident
                                   }
                    );
}


sub _parse_links { 
    my $self = shift;
    my $html = shift;

    my @titles = ( $html =~ m{<h2>(.*?)</h2>}gsi );
    my @tables = ( $html =~ m{<table class="datatable">(.*?)</table>}gsi );

    unless( @titles == @tables ) { 
        $self->logger->error( "Found different number of titles and tables" );
        return;
    }

    my @links;
    foreach my $tab( @tables ) { 
        my %link;
        $link{desc} = shift @titles;

        $tab =~ m{<a href="(?<url>.*?/tr[_-]\d{3}\.pdf)"}i;
        $link{link} = $+{url};

        push @links, \%link;
    }

    return @links;
}

1;
